/*
The MIT License

Copyright (c) 2010 Michael Webb

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */
//EL
(function() {
	var _call = function(args, scope, fn) {
		var slots = args.callee["__signal_slots__"].slice(0);
		var arglist = Array.prototype.slice.call(args,0);
		arglist.push(scope);
		for ( var j = 0; j < slots.length; j++) {
			var obj = slots[j][0];
			if (typeof obj === "undefined") {
				obj = scope;
			}
			var fun = slots[j][1];
			try {
				fun.apply(obj, arglist);
			} catch (e) {
			}
		}
		if (typeof fn === "function") {
			return fn.apply(scope, args);
		}
	};
	var _connect = function(fun, obj) {
		var slots = this["__signal_slots__"];
		slots.push( [ obj, fun ]);
	};
	var _disconnect = function(fun, obj) {
		var slots = this["__signal_slots__"];
		var i;
		for (i = 0; i < slots.length; i++) {
			if (obj == slots[i][0] && fun == slots[i][1]) {
				break;
			}
		}
		if (i < slots.length) {
			slots.splice(i, 1);
			return true;
		}
		return false;
	};
	var _disconnect_all = function() {
		arguments.callee["__signal_slots__"] = [];
	};
	/**
	 * @static
	 */
	var SignalFunction = {
		/**
		 * Connects a function to this signal
		 * @function Connect
		 * @param {Function} fn The function to be called on the signal
		 * @param {Object} scope The scope that fn should be called with.  If not defined, will recieve the scope of the signal.
		 */
		Connect : function(fn,scope){},
		/**
		 * Disconnects a function to this signal
		 * @function Disconnect
		 * @param {Function} fn The function connected to the signal
		 * @param {Object} scope The scope that fn was connected with
		 */
		Disconnect : function(fn,scope){},
		/**
		 * Disconnects all functions from this signal
		 * @function DisconnectAll
		 */
		DisconnectAll : function(){}		
	};
	/**
	 * A Factory function that returns a signalfunction object
	 * @function
	 * @param {Function} fn An optional function the signal wraps and uses for the return value
	 * @return {SignalFunction}
	 */
	CA.Signal = function(fn) {
		var _signal = function() {
			return _call(arguments, this, fn);
		};
		_signal["__signal_slots__"] = [];
		_signal["Connect"] = _connect;
		_signal["Disconnect"] = _disconnect;
		_signal["DisconnectAll"] = _disconnect_all;
		return _signal;
	};
})();
